//
//  CustomScrollView.m
//  lcf-newproject
//
//  Created by yangyang on 14-11-20.
//  Copyright (c) 2014年 yang. All rights reserved.
//

#import "CustomScrollView.h"

@interface CustomScrollView ()<UIScrollViewDelegate>{
    UIImageView *_leftImageView;
    UIImageView *_centerImageView;
    UIImageView *_rightImageView;
}


//@property (nonatomic,strong)UIScrollView * scrollView;
@property (nonatomic,strong)NSArray * imageUrlArray;
@property (nonatomic,strong)UIPageControl *pageControl;
@property (nonatomic,assign)NSInteger imageCount;
@property (nonatomic,assign)NSInteger currentImageIndex;

@end

@implementation CustomScrollView

-(id)initWithFrame:(CGRect)frame withImageData:(NSDictionary *)imageData{
    self = [super initWithFrame:frame];
    if (self) {
        _imageUrlArray = [imageData objectForKey:@"imageUrl"];
        _imageCount = _imageUrlArray.count;
        //设置当前显示的位置为中间图片
        [self setContentOffset:CGPointMake(__MainScreen_Width, 0) animated:NO];
        self.contentSize = CGSizeMake(_imageCount*__MainScreen_Width, ScrollViewHeight);
        self.pagingEnabled = YES;
        self.delegate = self;
        self.showsHorizontalScrollIndicator = NO;
        self.frame = frame;
        [self addImageToScrollView];
        [self addPageControl];
        [self setDefaultImage];

    }
    return self;
}


//添加图片到scrollView
-(void)addImageToScrollView{
    _leftImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, __MainScreen_Width, ScrollViewHeight)];
    _leftImageView.contentMode=UIViewContentModeScaleAspectFit;
    [self addSubview:_leftImageView];
    _centerImageView=[[UIImageView alloc]initWithFrame:CGRectMake(__MainScreen_Width, 0, __MainScreen_Width, ScrollViewHeight)];
    _centerImageView.contentMode=UIViewContentModeScaleAspectFit;
    [self addSubview:_centerImageView];
    _rightImageView=[[UIImageView alloc]initWithFrame:CGRectMake(2*__MainScreen_Width, 0, __MainScreen_Width, ScrollViewHeight)];
    _rightImageView.contentMode=UIViewContentModeScaleAspectFit;
    [self addSubview:_rightImageView];
}

#pragma mark 设置默认显示图片
-(void)setDefaultImage{
    //加载默认图片
    _leftImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"test%ld",_imageCount-1]];
    _centerImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"test%d",0]];
    _rightImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"test%d",1]];
    _currentImageIndex=0;
    //设置当前页
    _pageControl.currentPage=_currentImageIndex;
}


//添加分页控件
-(void)addPageControl{
    UIView *view= [[UIView alloc]initWithFrame:CGRectMake(0,ScrollViewHeight - 44, __MainScreen_Width, 44)];
    
    
    
    _pageControl=[[UIPageControl alloc]init];
    //注意此方法可以根据页数返回UIPageControl合适的大小
    CGSize size= [_pageControl sizeForNumberOfPages:_imageCount];
    _pageControl.bounds=CGRectMake(0, 0, size.width, size.height);
    _pageControl.center=CGPointMake(__MainScreen_Width/2, ScrollViewHeight-44);
    //设置颜色
    _pageControl.pageIndicatorTintColor=[UIColor colorWithRed:193/255.0 green:219/255.0 blue:249/255.0 alpha:1];
    //设置当前页颜色
    _pageControl.currentPageIndicatorTintColor=[UIColor colorWithRed:0 green:150/255.0 blue:1 alpha:1];
    //设置总页数
    _pageControl.numberOfPages=_imageCount;
    
    [self addSubview:_pageControl];
    
}

#pragma mark - UIScrollViewDelegate
//滚动停止事件
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //重新加载图片
    [self reloadImage];
    //移动到中间
    [self setContentOffset:CGPointMake(__MainScreen_Width, 0) animated:NO];
    //设置分页
    _pageControl.currentPage=_currentImageIndex;
}

#pragma mark - 重新加载图片

-(void)reloadImage{
    int leftImageIndex,rightImageIndex;
    CGPoint offset = [self contentOffset];
    if(offset.x>__MainScreen_Width){//向右滑动
        _currentImageIndex = (_currentImageIndex+1)%_imageCount;
    }else if (offset.x < __MainScreen_Width){//向左滑动
        _currentImageIndex = (_currentImageIndex + _imageCount -1)%_imageCount;
    }
    
    _centerImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"test%ld",(long)_currentImageIndex]];
    
    //重新设置左右图片
    leftImageIndex=(_currentImageIndex+_imageCount-1)%(int)_imageCount;
    rightImageIndex=(_currentImageIndex+1)%(int)_imageCount;
    _leftImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"test%d",leftImageIndex]];
    _rightImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"test%d",rightImageIndex]];
    
    
}

@end
